class Device {
  final String id;
  final String name;
  final String type;
  final bool status;
  final Map<String, dynamic> attributes;

  Device({
    required this.id,
    required this.name,
    required this.type,
    required this.status,
    required this.attributes,
  });

  Device copyWith({
    String? id,
    String? name,
    String? type,
    bool? status,
    Map<String, dynamic>? attributes,
  }) {
    return Device(
      id: id ?? this.id,
      name: name ?? this.name,
      type: type ?? this.type,
      status: status ?? this.status,
      attributes: attributes ?? Map<String, dynamic>.from(this.attributes),
    );
  }

  factory Device.fromJson(Map<String, dynamic> json) {
    return Device(
      id: json['id'],
      name: json['name'],
      type: json['type'],
      status: json['status'],
      attributes: Map<String, dynamic>.from(json['attributes'] ?? {}),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'type': type,
      'status': status,
      'attributes': attributes,
    };
  }

  @override
  String toString() {
    return 'Device(id: $id, name: $name, type: $type, status: $status, attributes: $attributes)';
  }

  // 获取设备类型的本地化名称
  String getTypeName() {
    switch (type) {
      case 'light':
        return '智能灯';
      case 'thermostat':
        return '温控器';
      case 'lock':
        return '智能锁';
      case 'camera':
        return '摄像头';
      case 'speaker':
        return '智能音箱';
      default:
        return '其他设备';
    }
  }

  // 获取设备状态的本地化描述
  String getStatusDescription() {
    switch (type) {
      case 'light':
        return status ? '已开启' : '已关闭';
      case 'thermostat':
        final temperature = attributes['temperature'] as double?;
        return status
            ? '运行中 - ${temperature?.toStringAsFixed(1) ?? "未知"}°C'
            : '已关闭';
      case 'lock':
        return status ? '已锁定' : '已解锁';
      case 'camera':
        return status ? '正在录制' : '已停止';
      case 'speaker':
        final volume = attributes['volume'] as int?;
        return status ? '播放中 - 音量${volume ?? 0}%' : '已静音';
      default:
        return status ? '开启' : '关闭';
    }
  }

  // 获取设备的主要属性描述
  String getMainAttributeDescription() {
    switch (type) {
      case 'light':
        final brightness = attributes['brightness'] as int?;
        return brightness != null ? '亮度: $brightness%' : '';
      case 'thermostat':
        final temperature = attributes['temperature'] as double?;
        return temperature != null ? '温度: ${temperature.toStringAsFixed(1)}°C' : '';
      case 'lock':
        return '';  // 智能锁没有主要属性显示
      case 'camera':
        final resolution = attributes['resolution'] as String?;
        return resolution != null ? '分辨率: $resolution' : '';
      case 'speaker':
        final volume = attributes['volume'] as int?;
        return volume != null ? '音量: $volume%' : '';
      default:
        return '';
    }
  }

  // 获取设备的次要属性描述
  List<String> getSecondaryAttributeDescriptions() {
    final List<String> descriptions = [];

    switch (type) {
      case 'light':
        final colorTemp = attributes['colorTemperature'] as int?;
        if (colorTemp != null) {
          descriptions.add('色温: ${colorTemp}K');
        }
        break;
      case 'thermostat':
        final humidity = attributes['humidity'] as int?;
        if (humidity != null) {
          descriptions.add('湿度: $humidity%');
        }
        final mode = attributes['mode'] as String?;
        if (mode != null) {
          descriptions.add('模式: $mode');
        }
        break;
      case 'camera':
        final nightVision = attributes['nightVision'] as bool?;
        if (nightVision != null) {
          descriptions.add('夜视: ${nightVision ? "开启" : "关闭"}');
        }
        final motionDetection = attributes['motionDetection'] as bool?;
        if (motionDetection != null) {
          descriptions.add('移动侦测: ${motionDetection ? "开启" : "关闭"}');
        }
        break;
      case 'speaker':
        final playing = attributes['playing'] as String?;
        if (playing != null) {
          descriptions.add('正在播放: $playing');
        }
        break;
    }

    return descriptions;
  }
}